var app = getApp();
var util = require("../../utils/util");
import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 联动的
    winHeight: "", //窗口高度
    currentTab:'', //预设当前项的值
    scrollLeft: "0", //tab标题的滚动条位置
    showLoading: true, //
    tabData: [{
        index: 0,
        name: "已点歌曲"
      },
      {
        index: 1,
        name: "历史演唱"
      },
      {
        index: 2,
        name: "我的收藏"
      },
    ],
    songlist0: [], //已点列表
    songlist1: [], //已唱列表
    songlist2: [], //收藏列表
    token: "", //用户token
    nowpage: 1,
    last_page0: "", //已点列表数据总页数
    last_page1: "", //已唱列表数据总页数
    last_page2: "", //收藏列表数据总页数
    alid: "", //已点列表删除歌曲的id
    songCode: "", //收藏列表删除的歌曲code
    listOneLoading: true, //已点列表是否正在加载
    listTwoLoading: true, //已点列表是否正在加载
    listThreeLoading: true, //已点列表是否正在加载
    isFullSucreen: app.globalData.isFullSucreen ? true : false, //判断机型
  },
  // 播放
  play(e) {
    console.log(e.currentTarget.dataset.code);
    console.log(e.currentTarget.dataset.id);
    var that = this;
    console.log(e.currentTarget.dataset.code);
    var token = wx.getStorageSync("token");
    var code = e.currentTarget.dataset.code;
    var alid = e.currentTarget.dataset.code;
    var tempParam = {
      code: code,
      alid: alid,
    };
    if (token) {
      util.postToken("/wechat/play", tempParam, token, function (res) {
        console.log(res.status_code, "播放");
        if (res.status_code == 200) {
          Toast.success("成功");
        } else {}
      });
    }
  },
  // 添加
  add(e) {
    var that = this;
    var codes = e.currentTarget.dataset.code;
    // "fa15884d-0ebb-388e-b179-363a077e089f"
    var uid = app.globalData.uid;
    // var uid = wx.getStorageSync("uid");
    var token = wx.getStorageSync("token");
    // console.log("avatarUrl:", wx.getStorageSync("avatarUrl"));
    var wxImg = wx.getStorageSync("wxImg");
    var tempParam = {
      codes: codes,
      uid: uid,
      headurl: wxImg,
    };
    if (token) {
      util.postToken("/wechat/playlist/song/add", tempParam, token, function (
        res
      ) {
        console.log(res, "添加");
        if (res.status_code == 200) {
          Toast.success("成功");
        }
      });
    } else {}
  },
  // 收藏
  love(e) {
    var that = this;
    var songCode = e.currentTarget.dataset.code;
    var token = wx.getStorageSync("token");
    var tempParam = {
      songCode: songCode,
    };
    if (token) {
      util.postToken("/wechat/song/favorites/add", tempParam, token, function (
        res
      ) {
        console.log(res, "收藏");
        if (res.status_code == 200) {
          Toast.success("成功");
        } else {
          Toast(res.msg);

        }
      });
    }
  },
  // 置顶
  first(e) {
    var that = this;
    //alid 歌曲id
    //uid
    // '/xcprioralreadysongs'
    let params = {
      alid: e.currentTarget.dataset.id,
      uid: app.globalData.uid,
    };
    util.postToken("/xcprioralreadysongs", params, that.data.token, function (
      res
    ) {
      console.log(res, "置顶");
      if (res.status_code == 200) {
        // 状态码200，直接修改数据不重新请求
        let arr = that.data.songlist0;
        let index = e.currentTarget.dataset.index;
        let item = arr.splice(index, 1);
        arr.unshift(item[0]);
        that.setData({
          songlist0: arr,
        });
      } else {}
    });
  },
  // 删除已点
  aldel(e) {
    var that = this;
    //alid   uid '/xcdeletealreadysongs'  删除已点
    that.setData({
      alid: e.currentTarget.dataset.id,
    });
    that.dialog2.show();
  },
  // 删除收藏
  lovedel(e) {
    var that = this;
    that.setData({
      songCode: e.currentTarget.dataset.code,
    });
    that.dialog2.show();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var token = wx.getStorageSync("token");
    var uid = wx.getStorageSync("uid");
    //  高度自适应
    // wx.getSystemInfo({
    //   success: function (res) {
    //     var clientHeight = res.windowHeight,
    //       clientWidth = res.windowWidth,
    //       rpxR = 750 / clientWidth;
    //     var calc = clientHeight * rpxR;
    //     console.log(calc);
    //     that.setData({
    //       winHeight: calc,
    //     });
    //   },
    // });
    if (options.index) {
      that.setData({
        currentTab:Number(options.index) ,
      });
    }else{
      that.setData({
        currentTab:0 ,
      });
    }
    that.getData();
  },
  //  获取列表数据
  getData: function (type) {
    var that = this;
    var token = wx.getStorageSync("token");
    that.setData({
      token: token,
      showLoading: true,
    });

    var url = "";
    if (type) {} else {
      that.setData({
        songlist0: [],
        songlist1: [],
        songlist2: [],
      });
    }
    // ----------------------------已点列表----------------------------
    if (that.data.currentTab == 0) {
      console.log("0");
      // 优化思路，不把当前data设为空，而是显示loading，等数据请求到了在替换数据
      // that.setData({
      //   songlist0: [],
      // });
      util.postToken(
        "/wechat/playlist", {
          per_page: 30,
          page: that.data.nowpage
        },
        token,
        function (res) {
          console.log(res.data, "已点列表");
          that.setData({
            songlist0: that.data.songlist0.concat(res.data.data),
            last_page0: res.data.last_page,
            showLoading: false,
          });
        }
      );
    } // ----------------------------已唱列表----------------------------
    else if (that.data.currentTab == 1) {
      console.log("1");
      // that.setData({
      //   songlist1: [],
      // });
      util.postToken(
        "/wechat/playedlist", {
          per_page: 30,
          page: that.data.nowpage
        },
        token,
        function (res) {
          console.log(res.data, "已唱列表");
          that.setData({
            songlist1: that.data.songlist1.concat(res.data.data),
            last_page1: res.data.last_page,
            showLoading: false,
          });
        }
      );
    } // ----------------------------收藏列表----------------------------
    else if (that.data.currentTab == 2) {
      console.log("2");
      // that.setData({
      //   songlist2: [],
      // });
      util.postToken(
        "/wechat/songs/favorites/list", {
          per_page: 30,
          page: that.data.nowpage
        },
        token,
        function (res) {
          console.log(res.data, "收藏列表");
          that.setData({
            songlist2: that.data.songlist2.concat(res.data.data),
            last_page2: res.data.last_page,
            showLoading: false,
          });
        }
      );
    }
  },
  onChange(event) {
    console.log(event.detail.index);
    this.setData({
      currentTab: event.detail.index,
      nowpage:1,
    });
    this.getData();
  },

  // 乱序
  disrupt() {
    var that = this;
    util.postToken("/wechat/playlist/disrupt", {}, that.data.token, function (
      res
    ) {
      that.getData();
    });
  },
  // 显示确认删除dialog
  showdialog1() {
    this.dialog1.show();
  },

  // dialog点击取消
  cancelEvent() {
    console.log("取消1");
  },
  cancelEvent2() {
    console.log("取消2");
  },

  // dialog点击确认
  okEvent() {
    var that = this;
    console.log("确认1");
    // 已点列表的清空
    if (that.data.currentTab == 0) {
      util.postToken(
        "/wechat/playlist/song/clear", {},
        that.data.token,
        function (res) {
          console.log(res, "清空已点");
          if (res.status_code == 200) {
            Toast.success("删除成功");
            that.getData();
          } else {}
        }
      );
    }
    //收藏列表的清空
    else if (that.data.currentTab == 2) {
      util.postToken(
        "/wechat/song/favorites/clear", {},
        that.data.token,
        function (res) {
          console.log(res, "清空收藏");
          if (res.status_code == 200) {
            Toast.success("删除成功");
            that.getData();
          } else {}
        }
      );
    }
  },
  okEvent2() {
    console.log("确认2");
    var that = this;
    // 删除已点
    if (that.data.currentTab == 0) {
      let params = {
        alid: that.data.alid,
        uid: app.globalData.uid,
      };
      util.postToken(
        "/wechat/playlist/song/remove",
        params,
        that.data.token,
        function (res) {
          console.log(res, "删除已点");
          if (res.status_code == 200) {
            Toast.success("删除成功");
            that.getData();
          } else {}
        }
      );
    }
    // 删除收藏
    else if (that.data.currentTab == 2) {
      let params = {
        songCode: that.data.songCode,
      };
      util.postToken(
        "/wechat/song/favorites/remove",
        params,
        that.data.token,
        function (res) {
          console.log(res, "删除收藏");
          if (res.status_code == 200) {
            Toast.success("删除成功");
            that.getData();
          } else {}
        }
      );
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 挂载dialog组件
    this.dialog1 = this.selectComponent("#dialog1");
    this.dialog2 = this.selectComponent("#dialog2");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(this.data.currentTab);
    let that = this;
    let index = this.data.currentTab;
    if (index == 0) {
      if (
        that.data.last_page0 >= 1 &&
        that.data.nowpage < that.data.last_page0
      ) {
        that.setData({
          nowpage: that.data.nowpage + 1,
        });
        that.getData("concat");
      }
    } else if (index == 1) {
      if (
        that.data.last_page1 >= 1 &&
        that.data.nowpage < that.data.last_page1
      ) {
        that.setData({
          nowpage: that.data.nowpage + 1,
        });
        that.getData("concat");
      }
    } else if (index == 2) {
      if (
        that.data.last_page2 >= 1 &&
        that.data.nowpage < that.data.last_page2
      ) {
        that.setData({
          nowpage: that.data.nowpage + 1,
        });
        that.getData("concat");
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});